import argparse
import os
import sys
from dotenv import load_dotenv
from functools import lru_cache
from pydantic_settings import BaseSettings
from common.config.db_env import DataBaseSettings, RedisSettings
from common.utils.log_util import logger

class AppSettings(BaseSettings):
    """
    应用配置
    """

    app_env: str = 'env'
    app_name: str = '快块低代码平台'
    app_root_path: str = '/'
    app_host: str = '0.0.0.0'
    app_port: int = 14004
    app_version: str = '1.0.0'
    app_reload: bool = True
    app_workers: int = 4
    app_ip_location_query: bool = True
    app_same_time_login: bool = True
class JwtSettings(BaseSettings):
    """
    Jwt配置
    """

    jwt_secret_key: str = 'UVqFB_utOFkRIR4LpAyimLAHhQKLDeP3nS2AgV84xnU'
    jwt_algorithm: str = 'HS256'
    jwt_expire_minutes: int = 1440
    jwt_redis_expire_minutes: int = 60

class UploadSettings:
    """
    上传配置
    """
    UPLOAD_PREFIX = '/profile'
    UPLOAD_PATH = 'kwzl_admin/upload_path'
    UPLOAD_MACHINE = 'A'
    DEFAULT_ALLOWED_EXTENSION = [
        # 图片
        'bmp',
        'gif',
        'jpg',
        'jpeg',
        'png',
        # word excel powerpoint
        'doc',
        'docx',
        'xls',
        'xlsx',
        'ppt',
        'pptx',
        'html',
        'htm',
        'txt',
        # 压缩文件
        'rar',
        'zip',
        'gz',
        'bz2',
        # 视频格式
        'mp4',
        'avi',
        'rmvb',
        # pdf
        'pdf',
    ]
    DOWNLOAD_PATH = 'kwzl_admin/download_path'

    def __init__(self):
        if not os.path.exists(self.UPLOAD_PATH):
            os.makedirs(self.UPLOAD_PATH)
        if not os.path.exists(self.DOWNLOAD_PATH):
            os.makedirs(self.DOWNLOAD_PATH)
class TokenSettings(BaseSettings):
    """
    token失效时间配置
    """
    token_expire_minutes: int = 60

class XcxLoginSettings(BaseSettings):
    xcx_app_id: str = ''
    xcx_app_secret :str = ''

class H5LoginSettings(BaseSettings):
    h5_app_id: str = ''
    h5_app_secret: str = ''

class CachePathConfig:
    """
    缓存目录配置
    """
    PATH = os.path.join(os.path.abspath(os.getcwd()), 'caches')
    PATHSTR = 'caches'


class GetConfig:
    """
    获取配置
    """
    def __init__(self):
        self.parse_cli_args()
    @lru_cache()
    def get_app_config(self):
        """
        获取应用配置
        """
        # 实例化应用配置模型
        return AppSettings()

    @lru_cache()
    def get_jwt_config(self):
        """
        获取Jwt配置
        """
        # 实例化Jwt配置模型
        return JwtSettings()

    @lru_cache()
    def get_database_config(self):
        # 实例化数据库配置模型
        return DataBaseSettings()

    @lru_cache()
    def get_redis_config(self):
        """
        获取Redis配置
        """
        # 实例化Redis配置模型
        return RedisSettings()

    @lru_cache()
    def get_token_config(self):
        """
        获取token失效时间配置
        """
        # 实例化token失效时间配置模型
        return TokenSettings()

    def get_XcxLoginSettings(self):
        return XcxLoginSettings()
    def get_H5LoginSettings(self):
        return H5LoginSettings()
    @lru_cache()
    def get_upload_config(self):
        """
        获取数据库配置
        """
        # 实例上传配置
        return UploadSettings()
    @staticmethod
    def parse_cli_args():
        logger.info(f'选择运行环境了')
        env_file = f'.env'
        logger.info(f'{env_file}重新加载环境配置')
        load_dotenv(env_file, override=True)



# 实例化获取配置类
get_config = GetConfig()
# 应用配置
AppConfig = get_config.get_app_config()
# Jwt配置
JwtConfig = get_config.get_jwt_config()
# 数据库配置
DataBaseConfig = get_config.get_database_config()
# Redis配置
RedisConfig = get_config.get_redis_config()
# 上传配置
UploadConfig = get_config.get_upload_config()
TokenConfig = get_config.get_token_config()
H5LoginConfig = get_config.get_H5LoginSettings()
XcxLoginConfig = get_config.get_XcxLoginSettings()
